package com.fzshang.api.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.commen.bean.Commodity;
import com.fzshang.commen.bean.layuiBean.ResultPage;
import com.fzshang.commen.bean.layuiBean.VoPage;
import com.fzshang.commen.controller.BaseController;
import com.fzshang.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-02-24
 */
@Controller
@RequestMapping("admin/commodity")
public class CommodityController extends BaseController {


    /**
     * @return 主页面
     */
    @RequestMapping(value = "commodity.html")
    public String commodity(Model model){
        //商家
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        model.addAttribute("user",userService.list(queryWrapper));
        //菜品分类
        model.addAttribute("varietyClassification",varietyClassificationService.list());
        return "admin/commodity/commodity";
    }

    /**
     * @return 添加页面
     */
    @RequestMapping(value = "commodity_add.html")
    public String commodity_add(Model model){
        //菜品分类
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",getUser().getId());
        if(getUser().getId()==1){
            model.addAttribute("varietyClassification",varietyClassificationService.list());
        }else{
            model.addAttribute("varietyClassification",varietyClassificationService.list(queryWrapper));
        }
        //属性分类
        model.addAttribute("attributeClassification",attributeClassificationService.list());
        return "admin/commodity/commodity_add";
    }

    /**
     * @param model
     * @param id
     * @return 编辑页面
     */
    @RequestMapping(value = "commodity_edit.html")
    public String commodity_edit(Model model, Long id){
        //商家
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        model.addAttribute("user",userService.list(queryWrapper));
        //菜品分类
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("user_id",getUser().getId());
        if(getUser().getId()==1){
            model.addAttribute("varietyClassification",varietyClassificationService.list());
        }else{
            model.addAttribute("varietyClassification",varietyClassificationService.list(queryWrapper1));
        }
        //属性分类
        model.addAttribute("attributeClassification",attributeClassificationService.list());
        model.addAttribute("commodity",commodityService.getById(id));
        return "admin/commodity/commodity_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Commodity> selectMyPage(VoPage voPage, Commodity commodity){
        return commodityService.selectMyPage(voPage,commodity,getUser().getId());
    }


    /**
     * @param prods
     * @return 添加
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public ApiResult save(String prods, String commodityName, String commodityUrl, Long classification){
        prods= StrUtil.removeAll(prods,"[],");
        prods= StrUtil.removeAll(prods,",[]");
        List<Commodity> commoditys = JSONObject.parseArray(prods,Commodity.class);
        for (int i = 0; i < commoditys.size(); i++) {
            Commodity commodity = new Commodity();
            commodity.setCommodityName(commodityName);
            commodity.setPrice(commoditys.get(i).getPrice());
            commodity.setClassification(classification);
            commodity.setCommodityUrl(commodityUrl);
            commodity.setCommodityDescribe(commoditys.get(i).getCommodityDescribe());
            commodity.setCommoditySpecifications(commoditys.get(i).getCommoditySpecifications());
            commodity.setOnTheShelf(commoditys.get(i).getOnTheShelf());
            commodity.setAttributeClassificationId(commoditys.get(i).getAttributeClassificationId());
            commodity.setAttributeId(commoditys.get(i).getAttributeId());
            commodity.setType(commoditys.get(i).getType());
            commodity.setUserId(getUser().getId());
            commodity.setCreateTime(System.currentTimeMillis());
            if(!commodityService.save(commodity)){
                return ApiResult.fail();
            }
        }
        return ApiResult.ok();
    }

    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(commodityService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param commodity
     * @return 修改
     */
    @RequestMapping(value = "updateById")
    @ResponseBody
    public ApiResult updateById(@RequestBody Commodity commodity){
        if(commodityService.updateById(commodity)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }



}
